﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using KPIS.GERP.Models;
using KPIS.GERP.Models.HRM;

namespace KPIS.GERP.WCF.GHRMS
{
    [ServiceContract]
    public interface IRuleAbsenceService
    {
        [OperationContract]
        List<RuleAbsence> Select();
        [OperationContract]
        RuleAbsence SelectOne(int ruleAbsenceId);
        [OperationContract]
        RuleAbsence Insert(RuleAbsence model, int byEmpId);
        [OperationContract]
        RuleAbsence Update(RuleAbsence model, int byEmpId);
        [OperationContract]
        RuleAbsence Delete(int ruleAbsenceId, int byEmpId);
    }

    public class RuleAbsenceService : IRuleAbsenceService
    {
        #region Implementation of IRuleAbsenceService
        GERPContext db = new GERPContext(false);

        public List<RuleAbsence> Select()
        {
            return db.RuleAbsences.Where(o => o.RecordStatus != "D").ToList();
        }

        public RuleAbsence SelectOne(int ruleAbsenceId)
        {
            return db.RuleAbsences.FirstOrDefault(o => o.RecordStatus != "D" && ruleAbsenceId == o.RuleAbsenceId);
        }

        public RuleAbsence Insert(RuleAbsence model, int byEmpId)
        {
            model.AbsenceType = null;
            model.EmpType = null;
            model.CreatedBy = byEmpId;
            model.CreatedWhen = DateTime.Now;
            db.RuleAbsences.Add(model);
            db.SaveChanges();
            return model;
        }

        public RuleAbsence Update(RuleAbsence model, int byEmpId)
        {
            model.AbsenceType = null;
            model.EmpType = null;
            model.UpdatedBy = byEmpId;
            model.UpdatedWhen= DateTime.Now;
            db.Entry(model).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();
            return model;
        }

        public RuleAbsence Delete(int ruleAbsenceId, int byEmpId)
        {
            var model = SelectOne(ruleAbsenceId);
            model.AbsenceType = null;
            model.EmpType = null;
            model.UpdatedBy = byEmpId;
            model.UpdatedWhen = DateTime.Now;
            db.Entry(model).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();
            return model;
        }

        #endregion
    }
}
